<!--
 * @Description: Description
 * @Author: Kerwin
 * @Date: 2025-04-01 15:30:01
 * @LastEditTime: 2025-04-02 10:46:18
 * @LastEditors:  Please set LastEditors
-->
<template>
    <view>
        <view class="message">
            <van-cell-group inset>
                <van-field v-model="message" rows="2" autosize type="textarea" maxlength="50" placeholder="请输入备注"
                    show-word-limit />
            </van-cell-group>
        </view>
        <view class="btns">
            <van-button plain hairline size="small" icon="minus" type="danger" @click="handleClear">
                清空备注
            </van-button>
            <van-button @click="handleAdd" size="small" icon="plus" type="primary">
                完成
            </van-button>

        </view>
        <view class="tags">
            <u-tag v-for="(item, index) in remarkList" :key="index" :text="item.note" type="info" mode="plain"
                @click="tagClick(item.note)" />
        </view>
    </view>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { onLoad, onShow, onReady } from '@dcloudio/uni-app'
//api
import { userApi } from '@/api';
//其他
import { useUserStore } from '@/store';
import { storeToRefs } from 'pinia';


const userStore = useUserStore()
const { userInfo } = storeToRefs(userStore)
const userId = ref(userInfo.value.id)
const message = ref('')
const remarkList = ref()
const $emit = defineEmits(['changeRemark'])




//#region ************************** 初始化数据 **************************/


//#endregion


//#region ************************** 处理数据 **************************/
onLoad(() => {
    console.log(userId.value);
    getRemark()
})
//查询备忘录
const getRemark = async () => {
    const { data } = await userApi.notesList({ userId: userId.value })
    console.log('备注列表', data);
    if (data.records) {
        remarkList.value = data.records
    } else {
        remarkList.value = []
    }

}

//点击赋值
const tagClick = (e: any) => {
    message.value = e
}


//添加
const handleAdd = async () => {
    message.value = message.value.trim()
    if (message.value) {
        const { data, code } = await userApi.notesAdd({
            note: message.value
        })
        if (code === 200) {
            // uni.showToast({
            //     title: '添加成功'
            // })
            getRemark()
            uni.$emit('updateData', message.value)
            uni.navigateBack({
                delta: 1
            })
        }
        console.log('添加备注', data, message.value);
    }

}


//一键清空
const handleClear = async () => {
    const { data, code } = await userApi.notesDeleteAll()
    if (code === 200) {
        uni.showToast({
            title: '清空成功'
        })
        getRemark()
    }
    console.log('清空备注', data);

}

//#endregion




</script>

<style scoped>
.message {
    margin-top: 20rpx;
}

.btns {
    display: flex;
    justify-content: space-between;
    padding: 30rpx;
}

.tags {
    padding: 30rpx;
}

:deep(.u-size-default) {
    margin: 10rpx;
}
</style>